//
//  MineViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/11.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "SettingViewController.h"
#import "UpdateUserInfoViewController.h"
#import "MineHeadView.h"
#import "BLHeader.h"
#import "User.h"
#import <Masonry.h>
#import "SellViewController.h"
#import "SuggestViewController.h"
#import "ProfitViewController.h"
#import "MYQRCoderViewController.h"

@interface MineViewController () <UITableViewDataSource, UITableViewDelegate, MineHeadViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (strong, nonatomic) NSArray *titleNames;
@property (strong, nonatomic) NSArray *imageNames;
@property (strong, nonatomic) MineHeadView *headView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void) setupUI {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger userClass = [[userDefault objectForKey:USERCLASS] integerValue];
    if(userClass == 1) { // 1 店主 2 销售
        self.titleNames = @[@"我的销售",@"收益", @"设置", @"意见反馈"];
        self.imageNames = @[@"sell", @"profit", @"setting",@"suggestion"];
    }else if (userClass == 2){
        self.titleNames = @[@"我的店主",@"收益",@"设置", @"意见反馈"];
        self.imageNames = @[@"sell", @"profit", @"setting",@"suggestion"];
    }else {
        self.titleNames = @[@"我的二维码",@"设置", @"意见反馈"];
        self.imageNames = @[@"QRCode", @"setting", @"suggestion"];
    }
    [self.table registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
    self.headView = [MineHeadView share];
    self.headView.delegate = self;
    self.headView.frame = CGRectMake(0, 0, KScreenWidth, 200);
    self.table.tableHeaderView = self.headView;
    self.table.tableFooterView = [UIView new];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table reloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationUpdateHead:) name:@"updateHeadView" object:nil];
}

#pragma mark 修改用户信息
- (void)modifyUserInfo {
    UpdateUserInfoViewController *updateController = [[UpdateUserInfoViewController alloc] init];
    [updateController setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:updateController animated:YES];
    [updateController setHidesBottomBarWhenPushed:NO];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleNames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
    [cell setupCell:self.titleNames[indexPath.row] imageName:self.imageNames[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIViewController *controller;
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger userClass = [[userDefault objectForKey:USERCLASS] integerValue];
    if(userClass == 3) { //顾客
        if(indexPath.row == 0) {
            controller = [[MYQRCoderViewController alloc] init];
        }else if(indexPath.row == 1) {
            controller  = [[SettingViewController alloc] init];
        }else if(indexPath.row == 2) {
            controller = [[SuggestViewController alloc] init];
        }
    }else {//店主 ， 销售
        if(indexPath.row == 0) {
            controller = [[SellViewController alloc] init];
        }else if (indexPath.row == 1) {
            controller = [[ProfitViewController alloc] init];
        }else if(indexPath.row == 2) {
            controller  = [[SettingViewController alloc] init];
        }else if(indexPath.row == 3) {
            controller = [[SuggestViewController alloc] init];
        }
    }
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)notificationUpdateHead:(NSNotification *) notification {
    UIImage *image = notification.object;
    [self.headView updateHead:image];
}

@end
